package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoSheHuiShiJianTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.DoUploadFileTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 做大作业
 */
public class DoUploadFile {


    public static void main(String[] args) throws Exception {


     //   Thread.sleep(1100*60*60*1);
        int sync = 4;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

        // InputStream inputStream2 = new FileInputStream("libs//lms2022-x.user");
      //  InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//all.user");
        InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//all3.user");
     //   InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//李俊杰2.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];
            LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
            lmsWatchDTO.setUsername(userCode);
            lmsWatchDTO.setPassword(pwd);

            Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);
            if (null == courseMap) {
                continue;
            }
            Map<Long, String> newCourseMap = Maps.newHashMap();

            for (Long cId : courseMap.keySet()) {
                String subjectName = courseMap.get(cId);
                if (Strings.isNullOrEmpty(subjectName)) {
                    continue;
                }
                if (subjectName.equals("计算机应用基础")) {
                    newCourseMap.put(cId, subjectName);
                }
            }
            if(newCourseMap.size()<1){
                continue;
            }

            Map<String,Set<String>> urlMap = Maps.newHashMap();
            urlMap.put("计算机应用基础", Sets.newHashSet("https://lms.ouchn.cn/course/10000001720/learning-activity/full-screen#/10000209779"));
            lmsWatchDTO.setUrlMap(urlMap);
            lmsWatchDTO.setCourseMap(newCourseMap);

            String filePath = "libs//lmsAnswerInfo//"+ "计算机应用基础";

            if (!new File(filePath).exists()) {
                new File(filePath).mkdirs();
            }

            filePath = filePath + "//" + userCode + "-upload.txt";

            File completeInfoFile = new File(filePath);
            if (!completeInfoFile.exists()) {
                completeInfoFile.createNewFile();
            }
            InputStream inputStream = new FileInputStream(completeInfoFile);
            String completeInfoStr = IOUtils.toString(inputStream, "UTF-8");
            List<CompleteInfo> completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
            if (null == completeInfoList) {
                completeInfoList = Lists.newArrayList();
            }

            List<String> haveCompleteHomeWorkUrlSet = Lists.newArrayList();

            for (CompleteInfo info : completeInfoList) {
                if (info.isComplete()) {
                    haveCompleteHomeWorkUrlSet.add(info.getHomeworkUrl());
                }
            }

            boolean isReturn = true;
            for(String url:Sets.newHashSet("https://lms.ouchn.cn/course/10000001720/learning-activity/full-screen#/10000209779")){
                if(!haveCompleteHomeWorkUrlSet.contains(url)){
                    isReturn = false;
                    break;
                }
            }
            if(isReturn){
                continue;
            }


            watchQueue.add(lmsWatchDTO);
            System.err.println("lmsWatchDTO:"+ JSON.toJSONString(lmsWatchDTO));

        }
    //    Thread.sleep(2000000000);

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new DoUploadFileTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }
    }


}
